var alertUI = (function() {

	var timer;
	var $alertTips = $('#alertTips');
	var $alertTipsInner = $('#alertTipsInner');

	function init(text, time) {
		var time = time || 1000;

		clearTimeout(timer);
		$alertTipsInner[0].innerHTML = text;
		$alertTips.show();
		timer = setTimeout(function() {
			$alertTips.hide();
		}, time);

	}

	return {
		init : init
	}

}());

/**
 * ============================================================================
 * toggle
 * ============================================================================
 */
var toggle = (function($) {

	function toggleFun($self, other) {
		var show = false;
		var $parent = $self.parent();

		if (!$parent.hasClass("active")) {
			show = true;
		}

		$(other).removeClass("active");

		if (show) {
			$parent.addClass("active");
		}
	}

	$("body").on("click", "[data-toggle='toggle']", function(e) {
		e.preventDefault();

		var $self = $(this);
		var other = $self.attr("data-toggle-box");

		toggleFun($self, other);
	});

	return {
		toggleFun : toggleFun
	};

}(Zepto));

/**
 * ============================================================================
 * 获取数据
 * ============================================================================
 */
var modelModule = (function() {

	var dataUrl = '../common/CommonCtrl/getProductInfoById';

	function dataCallback(json, $tmpl, $appedEL, fn) {

		$appedEL[0].innerHTML = doT.compile($tmpl[0].innerHTML)(json);

		fn && fn();
	}

	function getData() {
		var data = {
			'id' : wxUtils.getParamsByUrl('id')
		};
		$
				.ajax({
					type : 'POST',
					url : dataUrl,
					contentType : 'application/json',
					data : JSON.stringify(data),
					dataType : 'json',
					success : function(json) {
						if (json.resultCode == '200') {
							var result = json.result;
							// 预期收益
							var incomeDisType = result.incomeDisType;
							$('#incomeDisType').html(incomeDisType);
							// 风险等级
							$('#riskLevel').html(
									getDescByCode
											.getRiskLevel(result.riskLevel));
							// 起购金额
							var subscriptionOrigin = (result.subscriptionOrigin == null ? ''
									: result.subscriptionOrigin + "万元");
							$('#subscriptionOrigin').html(subscriptionOrigin);
							// 剩余额度
							var sraiseAccount = (result.sraiseAccount == null ? ''
									: result.sraiseAccount);
							$('#sraiseAccount').html(sraiseAccount);

							// 期限
							if (result.proTerm != null) {
								var proTerm = result.proTerm;
								if (proTerm > 360) {
									proTerm = proTerm / 360 + "年";
								} else if (proTerm > 30) {
									proTerm = proTerm / 30 + "月";
								} else {
									proTerm = proTerm + "天";
								}
								$('#proTerm').html(proTerm);
							}
							// 总额
							var financingAccount = (result.financingAccount == null ? ''
									: result.financingAccount + '元');
							$('#financingAccount').html(financingAccount);
							// 进度
							var ycount = result.yraiseAccount; // 已募集的金额
							var financingAccount = result.financingAccount;
							var rate = '0%';
							if (ycount == financingAccount) {
								rate = '100%';
							} else {
								var num01 = (ycount/financingAccount).toFixed(2);
		 						var rate = Math.round((num01 * 10000)/100).toFixed(2) + "%";
							}
							$('#percentTxt').html(rate);
							$('#progress').css('width', rate);

							// 募集时间
							var raiseBeginDate = result.raiseBeginDate;
							var raiseEndDate = result.raiseEndDate;
							if (raiseBeginDate != null ) {
								var beginDate = new Date(raiseBeginDate).format("yyyy-MM-dd");
								var endDate = new Date(raiseEndDate).format("yyyy-MM-dd");
								var date = beginDate + "   至   " + endDate
								$('#raiseDate').html(date);
							}

							 

							var json = {
								title1 : '年化收益率',
								content1 : result.incomeTxt,
								title2 : '产品介绍',
								content2 : result.introduceTxt,
								title3 : '产品要素',
								content3 : {
									c_title1 : '【资金用途】',
									c_content1 : result.fundUse == null ? ''
											: result.fundUse,
									c_title2 : '【产品发行方式】',
									c_content2 : getDescByCode
											.getPublishType(result.publishType),
									c_title3 : '【债权推荐方】',
									c_content3 : result.claimRecommend == null ? ''
											: result.claimRecommend,
									c_title4 : '【债权转让服务方】',
									c_content4 : result.creditAssignment == null ? ''
											: result.creditAssignment,
									c_title5 : '【债权转让服务方介绍】',
									c_content5 : result.creditAssignmentInfo == null ? ''
											: result.creditAssignmentInfo,
									c_title6 : '【回款来源】',
									c_content6 : result.paymentSource == null ? ''
											: result.paymentSource,
									c_title7 : '【债权推荐方介绍】',
									c_content7 : result.claimRecommendInfo == null ? ''
											: result.claimRecommendInfo
								},
								title4 : '产品风控',
								content4 : result.riskctrlTxt,
							};
							dataCallback(json, $('#panelListTmpl'),
									$('#panelList'));
						} else {
							alertUI.init('网络繁忙，请稍后再试');
						}
					},
					error : function() {
						alertUI.init('网络繁忙，请稍后再试');
					}
				});
	}

	function init() {
		getData();
		var json = {
			title1 : '年化收益率',
			content1 : '',
			title2 : '产品介绍',
			content2 : '',
			title3 : '产品要素',
			content3 : '暂无',
			title4 : '产品风控',
			content5 : '',
		};
		dataCallback(json, $('#panelListTmpl'), $('#panelList'));
	}

	init();

}());


